package com.hsck.order.kds.main.fragment.order

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.OrderDynamicsDishItemsLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class OrderDynamicsDishItemsAdapter(val activity: Activity) :
    RecyclerView.Adapter<OrderDynamicsDishItemsAdapter.ItemVH>() {

    private var binding: OrderDynamicsDishItemsLayoutBinding? = null
    private val diffUtil = object : DiffUtil.ItemCallback<TestListBean>() {
        override fun areItemsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem == newItem
        }
    }
    private val asyncListDiffer = AsyncListDiffer(this, diffUtil)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        binding = OrderDynamicsDishItemsLayoutBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ItemVH(binding!!)
    }

    inner class ItemVH(val binding: OrderDynamicsDishItemsLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bindData(position: Int) {
            val item = asyncListDiffer.currentList[position]
            binding.bean = item
//            binding.tvTime.text = calculateMinutesSinceOrder(item.time).toString()
            itemView.setOnSingleClickListener {
                onDishOperationItemListener.onDishOperationItemClick(item, position, asyncListDiffer.currentList.size - 1)
            }
        }
    }

    fun submitList(dataResponse: List<TestListBean>) {
        asyncListDiffer.submitList(dataResponse)
    }

    fun addItemAfterId(newItem: TestListBean, targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.add(targetIndex, newItem)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterId(targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterIndex(targetIndex: Int) {
        if (targetIndex < asyncListDiffer.currentList.size) {
            val newList = asyncListDiffer.currentList.toMutableList()
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }


    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(position)
    }

    override fun getItemCount(): Int {
        return asyncListDiffer.currentList.size
    }


    private lateinit var onDishOperationItemListener: DishOperationItemClickListener
    fun setDishOperationItemClickListener(onDishOperationItemClickListener: DishOperationItemClickListener) {
        this.onDishOperationItemListener = onDishOperationItemClickListener
    }

    fun setTextColor(type: Int) {
        when (type) {
            0 -> {
                if (binding?.tvDishName?.tag != "enchanting") {
                    asyncListDiffer.currentList.forEach {
                        it.timeType = 0
                    }
                    binding?.tvDishName?.tag = "enchanting"
                }
            }

            1 -> {
                if (binding?.tvDishName?.tag != "ring") {
                    asyncListDiffer.currentList.forEach {
                        it.timeType = 1
                    }
                    binding?.tvDishName?.tag = "ring"
                }
            }

            2 -> {
                if (binding?.tvDishName?.tag != "chaos") {
                    asyncListDiffer.currentList.forEach {
                        it.timeType = 2
                    }
                    binding?.tvDishName?.tag = "chaos"
                }
            }
        } }

    interface DishOperationItemClickListener {
        fun onDishOperationItemClick(itemBean: TestListBean, childPosition: Int, childListSize: Int)
    }
}